package com.chuangshu.chuangshuteam.config;

import com.chuangshu.chuangshuteam.entity.Role;
import com.chuangshu.chuangshuteam.mapper.RoleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void run(String... args) throws Exception {
        initializeRoles();
    }

    private void initializeRoles() {
        List<Role> roles = Arrays.asList(
                new Role().setName("admin").setDescription("系统管理员"),
                new Role().setName("leader").setDescription("方向负责人"),
                new Role().setName("member").setDescription("普通成员")
        );

        for (Role role : roles) {
            Role existing = roleMapper.selectOne(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Role>()
                    .eq("name", role.getName()));
            if (existing == null) {
                roleMapper.insert(role);
                log.info("初始化角色: {}", role.getName());
            }
        }
    }
}